

package org.tools;

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class CharChangeSet
{

	public CharChangeSet()
	{
	}

	public static byte[] hexStringToByte(String hex)
	{
		int len = hex.length() / 2;
		byte result[] = new byte[len];
		char achar[] = hex.toCharArray();
		for (int i = 0; i < len; i++)
		{
			int pos = i * 2;
			result[i] = (byte)(toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}

		return result;
	}

	private static byte toByte(char c)
	{
		byte b = (byte)"0123456789ABCDEF".indexOf(c);
		return b;
	}

	public static final String bytesToHexString(byte bArray[])
	{
		StringBuffer sb = new StringBuffer(bArray.length);
		for (int i = 0; i < bArray.length; i++)
		{
			String sTemp = Integer.toHexString(0xff & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}

		return sb.toString();
	}

	public static final Object bytesToObject(byte bytes[])
		throws IOException, ClassNotFoundException
	{
		ByteArrayInputStream in = new ByteArrayInputStream(bytes);
		ObjectInputStream oi = new ObjectInputStream(in);
		Object o = oi.readObject();
		oi.close();
		return o;
	}

	public static final byte[] objectToBytes(Serializable s)
		throws IOException
	{
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ObjectOutputStream ot = new ObjectOutputStream(out);
		ot.writeObject(s);
		ot.flush();
		ot.close();
		return out.toByteArray();
	}

	public static String hexToHexString(byte input)
	{
		String str = input <= 15 ? "0" : "";
		str = (new StringBuilder(String.valueOf(str))).append(Integer.toHexString(input)).toString();
		if (str.length() > 2)
			str = str.substring(str.length() - 2);
		return str;
	}

	public static String hexToSepHexString(byte data[])
	{
		if (data != null)
		{
			StringBuffer result = new StringBuffer();
			for (int i = 0; i < data.length; i++)
			{
				result.append(hexToHexString(data[i]));
				if (i < data.length - 1)
					result.append("-");
			}

			return result.toString();
		} else
		{
			return "";
		}
	}

	public static final String objectToHexString(Serializable s)
		throws IOException
	{
		return bytesToHexString(objectToBytes(s));
	}

	public static final Object hexStringToObject(String hex)
		throws IOException, ClassNotFoundException
	{
		return bytesToObject(hexStringToByte(hex));
	}

	public static String bcd2Str(byte bytes[])
	{
		StringBuffer temp = new StringBuffer(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++)
		{
			temp.append((byte)((bytes[i] & 0xf0) >>> 4));
			temp.append((byte)(bytes[i] & 0xf));
		}

		return temp.toString();
	}

	public static String cbcd2string(byte b[])
	{
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < b.length; i++)
		{
			int h = ((b[i] & 0xff) >> 4) + 48;
			sb.append((char)h);
			int l = (b[i] & 0xf) + 48;
			sb.append((char)l);
		}

		return sb.toString();
	}

	public static String bcd2StrTime(byte bytes[])
	{
		StringBuffer temp = new StringBuffer(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++)
		{
			temp.append((byte)((bytes[i] & 0xf0) >>> 4));
			temp.append((byte)(bytes[i] & 0xf));
		}

		return temp.toString();
	}

	public static byte[] str2Bcdteleph(String asc)
	{
		int len = asc.length();
		int mod = len % 2;
		if (mod != 0)
		{
			asc = (new StringBuilder(String.valueOf(asc))).append("F").toString();
			len = asc.length();
		} else
		{
			asc = (new StringBuilder(String.valueOf(asc))).append("FF").toString();
			len = asc.length();
		}
		byte abt[] = new byte[len];
		if (len >= 2)
			len /= 2;
		byte bbt[] = new byte[len];
		abt = asc.getBytes();
		for (int p = 0; p < asc.length() / 2; p++)
		{
			int j;
			if (abt[2 * p] >= 48 && abt[2 * p] <= 57)
				j = abt[2 * p] - 48;
			else
			if (abt[2 * p] >= 97 && abt[2 * p] <= 122)
				j = (abt[2 * p] - 97) + 10;
			else
				j = (abt[2 * p] - 65) + 10;
			int k;
			if (abt[2 * p + 1] >= 48 && abt[2 * p + 1] <= 57)
				k = abt[2 * p + 1] - 48;
			else
			if (abt[2 * p + 1] >= 97 && abt[2 * p + 1] <= 122)
				k = (abt[2 * p + 1] - 97) + 10;
			else
				k = (abt[2 * p + 1] - 65) + 10;
			int a = (j << 4) + k;
			byte b = (byte)a;
			bbt[p] = b;
		}

		System.out.println((new StringBuilder("bbt�ĳ���Ϊ")).append(bbt.length).toString());
		return bbt;
	}

	public static byte[] str2Bcd(String asc)
	{
		int len = asc.length();
		int mod = len % 2;
		if (mod != 0)
		{
			asc = (new StringBuilder("0")).append(asc).toString();
			len = asc.length();
		}
		byte abt[] = new byte[len];
		if (len >= 2)
			len /= 2;
		byte bbt[] = new byte[len];
		abt = asc.getBytes();
		for (int p = 0; p < asc.length() / 2; p++)
		{
			int j;
			if (abt[2 * p] >= 48 && abt[2 * p] <= 57)
				j = abt[2 * p] - 48;
			else
			if (abt[2 * p] >= 97 && abt[2 * p] <= 122)
				j = (abt[2 * p] - 97) + 10;
			else
				j = (abt[2 * p] - 65) + 10;
			int k;
			if (abt[2 * p + 1] >= 48 && abt[2 * p + 1] <= 57)
				k = abt[2 * p + 1] - 48;
			else
			if (abt[2 * p + 1] >= 97 && abt[2 * p + 1] <= 122)
				k = (abt[2 * p + 1] - 97) + 10;
			else
				k = (abt[2 * p + 1] - 65) + 10;
			int a = (j << 4) + k;
			byte b = (byte)a;
			bbt[p] = b;
		}

		return bbt;
	}

	public static String MD5EncodeToHex(String origin)
	{
		return bytesToHexString(MD5Encode(origin));
	}

	public static byte[] MD5Encode(String origin)
	{
		return MD5Encode(origin.getBytes());
	}

	public static byte[] MD5Encode(byte bytes[])
	{
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
			return md.digest(bytes);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return new byte[0];
		}
	}

	public static String byteToHex(int j)
	{
		int i = j & 0xff;
		String s = Integer.toHexString(i);
		if (s.length() % 2 != 0)
			s = (new StringBuilder("0")).append(s).toString();
		return s;
	}

	public static String toHexString(String s)
	{
		String str = "";
		for (int i = 0; i < s.length(); i++)
		{
			int ch = s.charAt(i);
			String s4 = Integer.toHexString(ch);
			str = (new StringBuilder(String.valueOf(str))).append(s4).toString();
		}

		return str;
	}

	public static byte uniteBytes(byte src0, byte src1)
	{
		byte _b0 = Byte.decode((new StringBuilder("0x")).append(new String(new byte[] {
			src0
		})).toString()).byteValue();
		_b0 <<= 4;
		byte _b1 = Byte.decode((new StringBuilder("0x")).append(new String(new byte[] {
			src1
		})).toString()).byteValue();
		byte ret = (byte)(_b0 ^ _b1);
		return ret;
	}

	public static byte[] HexString2Bytes(String src)
	{
		byte ret[] = new byte[8];
		byte tmp[] = src.getBytes();
		for (int i = 0; i < 8; i++)
			ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);

		return ret;
	}

	public static String hex2bin(String hex)
	{
		String digital = "0123456789ABCDEF";
		char hex2char[] = hex.toCharArray();
		byte bytes[] = new byte[hex.length() / 2];
		for (int i = 0; i < bytes.length; i++)
		{
			int temp = digital.indexOf(hex2char[2 * i]) * 16;
			temp += digital.indexOf(hex2char[2 * i + 1]);
			bytes[i] = (byte)(temp & 0xff);
		}

		return new String(bytes);
	}

	public static String toStringHexTest(String s)
	{
		byte baKeyword[] = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++)
			try
			{
				baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}

		try
		{
			s = new String(baKeyword, "utf-8");
		}
		catch (Exception e1)
		{
			e1.printStackTrace();
		}
		return s;
	}
}
